#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

enum steering_cmd {
	VERSION,
	SPEED0,
	SPEED7,
	DEGREE0,
	DEGREE90,
	DEGREE180,
	POS_READ,
	STAT_READ,
	BUT_READ,
	LED_ON,
	LED_OFF,
	STEERING_MIX
};

unsigned char steering_control[]={
    0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0a,     //读取软件版本号
    0x01, 0x06, 0x00, 0x02, 0x00, 0x00, 0x28, 0x0a,     //设置舵机速度0（最慢）
    0x01, 0x06, 0x00, 0x02, 0x00, 0x07, 0x69, 0xC8,     //设置舵机速度0（最快） 
    0x01, 0x06, 0x00, 0x06, 0x01, 0xf4, 0x69, 0xdc,     //舵机转到0度（位置值500） 
    0x01, 0x06, 0x00, 0x06, 0x05, 0xdc, 0x6b, 0x02,     //舵机转到90度（位置值1500） 
    0x01, 0x06, 0x00, 0x06, 0x09, 0xc4, 0x6e, 0x08,     //舵机转到180度（位置值2500）   
    0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b,     //读舵机的位置 
    0x01, 0x03, 0x00, 0x0a, 0x00, 0x01, 0xa4, 0x08,     //读所有舵机运动状态 
    0x01, 0x03, 0x00, 0x0b, 0x00, 0x01, 0xf5, 0xc8,     //读按钮自上次读取后是否被按下过    
    0x01, 0x06, 0x00, 0x0c, 0x00, 0x01, 0x88, 0x09,     //设置用户LED灯开   
    0x01, 0x06, 0x00, 0x0c, 0x00, 0x00, 0x49, 0xc9,     //设置用户LED灯关   
};

enum uart_port {
	COMPORT1 = 1,
	COMPORT2 = 2,
	COMPORT_MIX
};

enum check_event {
	NONE,
	EVEN,
	ODD,
	EVENT_MIX
};

struct uart_desc {
	enum uart_port port;
	unsigned int baudrate;
	unsigned char nbits;
	unsigned char stopbits;
	enum check_event event;
};

int main(int argc, char **argv)
{
	struct uart_desc uart;
	int fd;

	// 输入合法性检验
	if (argc < 3 || atoi(argv[1]) != uart.port) {
		fprintf(stderr, "Usage: ./steering 2 degree");
		exit(1);
	}
	
	fd = uart_open(uart.port);
	if (-1 == fd) {
		fprintf(stderr, "Failed to open UART!\n");
		exit(1);
	}

	uart.baudrate = 115200;
	uart.nbits = 8;
	uart.event = NONE;
	uart.stopbits = 1;
	
	uart_setting(fd, uart);

	steering_ctl(fd, buf, VERSION);



	return 0;
}

static int uart_open(int com)
{
	int fd;
	char buf[BUFSIZE] = {};

	snprintf(buf, BUFSIZE, "/dev/ttyAMA%d", com);
	fd = open(buf, O_RDWR | O_NOCTTY | O_NDELAY);
	

}

